package com.bocai.eelly.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;

import com.bocai.eelly.R;
import com.bocai.eelly.bean.TabEntity;
import com.bocai.eelly.fragments.HistoryFragment;
import com.bocai.eelly.fragments.HomeFragment;
import com.bocai.eelly.fragments.InformationFragment;
import com.bocai.eelly.fragments.UserFragment;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * Created by kaizen on 4/1/16.
 */
public class HomeActivity extends AppCompatActivity {

    public final static String ID = "id";
    public final static String NAME = "name";
    public final static String TYPE = "type";
    public final static int GROUP = 1;
    public final static int PRIVATE = 2;
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    @Bind(R.id.ctl_footer)
    CommonTabLayout footer;
    private int[] mIconUnSelectIds = {
            R.mipmap.ic_home_unselecte, R.mipmap.ic_information_unselecte,
            R.mipmap.ic_history_unselecte, R.mipmap.ic_me_unselecte,
    };
    private int[] mIconSelectIds = {
            R.mipmap.ic_home_selecte, R.mipmap.ic_information_selecte,
            R.mipmap.ic_history_selecte, R.mipmap.ic_me_selecte,
    };
    private String[] mTitles = {"首页", "资讯", "历史", "我的"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        resetFragments(savedInstanceState);
        ButterKnife.bind(this);
        initEvent();
    }

    private void resetFragments(Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            List<Fragment> fragments = getSupportFragmentManager().getFragments();
            if (fragments != null && fragments.size() > 0) {
                boolean showFlag = false;
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                for (int i = fragments.size() - 1; i >= 0; i--) {
                    Fragment fragment = fragments.get(i);
                    if (fragment != null) {
                        if (!showFlag) {
                            transaction.show(fragments.get(i));
                            showFlag = true;
                        } else {
                            transaction.hide(fragments.get(i));
                        }
                    }
                }
                transaction.commitAllowingStateLoss();
            }
        }
    }

    private void initEvent() {
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnSelectIds[i]));
        }
        mFragments.add(new HomeFragment());
        mFragments.add(new InformationFragment());
        mFragments.add(new HistoryFragment());
        mFragments.add(new UserFragment());
        footer.setTabData(mTabEntities, this, R.id.container, mFragments);
        footer.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                footer.hideMsg(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.container);
        if (fragment != null && fragment instanceof UserFragment) {
            fragment.onActivityResult(requestCode, resultCode, data);
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    protected void onDestroy() {
        mFragments.clear();
        super.onDestroy();
    }
}
